 
using B.S.Data.MES.API;
using B.S.Infratrctrue;
using CSRedis;
using FluentAssertions.Common;
using Hangfire;
using Hangfire.MySql;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using NLog.Web;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;
using Yitter.IdGenerator;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(o =>
{
    // ˽Կ
    var secretByte = Encoding.UTF8.GetBytes("kdsfldsflkdslkflkdsflkdslfdslkflk");
    o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
    {
        // ��֤������
        ValidateIssuer = true,
        // ��������Ϣ
        ValidIssuer = "BeiJingBW",

        // ��֤������
        ValidateAudience = true,
        // ������
        ValidAudience = "BeiJingBW",

        // ��֤�Ƿ����
        ValidateLifetime = true,

        // ��֤˽Կ
        IssuerSigningKey = new SymmetricSecurityKey(secretByte)

    };
});

// Add services to the container.

builder.Services.AddControllers(x =>
{
    x.Filters.Add(typeof(MESGuoFilters));
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
RedisHelper.Initialization(new CSRedisClient(builder.Configuration.GetConnectionString("redis")));
builder.Services.AddSwaggerGen(o =>
{
    var tran = AppDomain.CurrentDomain.BaseDirectory + "B.S.Data.MES.API.Xml";
    o.IncludeXmlComments(tran, true);
    o.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme()
    {
        Description = "����JWT��ȨToken��Bearer Tokenֵ",
        Name = "Authorization",
        In = Microsoft.OpenApi.Models.ParameterLocation.Header,
        Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
        BearerFormat = "JWT",
        Scheme = "Bearer"
    });
    o.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
            },
            new string[]
            {

            }
        }
    });
});


builder.Logging.AddConsole(consoleLogOptions =>
{
    consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});

builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();

builder.Services.AddDbContext<SysDbcontext>(x => x.UseMySql(builder.Configuration.GetConnectionString("con"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("con"))));
//����ע��ִ�
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
// 注册AI客服数据查询服务
builder.Services.AddScoped<B.S.Data.MES.API.Services.IDataQueryService, B.S.Data.MES.API.Services.DataQueryService>();

// 注册科大讯飞配置
builder.Services.Configure<B.S.Data.MES.API.Models.XunFeiConfig>(
    builder.Configuration.GetSection("XunFei"));

// 注册科大讯飞AI服务
builder.Services.AddScoped<B.S.Data.MES.API.Services.IXunFeiService, B.S.Data.MES.API.Services.XunFeiService>();
// 注册天气服务
builder.Services.AddScoped<B.S.Data.MES.API.Services.IWeatherService, B.S.Data.MES.API.Services.WeatherService>();
YitIdHelper.SetIdGenerator(new IdGeneratorOptions(0));
builder.Host.UseNLog();
builder.Services.AddHttpClient();


builder.Services.AddSignalR();

builder.Services.AddMediatR(x => x.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
builder.Services.AddAutoMapper(typeof(MESMapping));
var app = builder.Build();
app.UseStaticFiles();
app.UseCors(a => a.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
    app.UseSwagger();
    app.UseSwaggerUI();
//}
app.UseCors("AllowAll");
app.UseAuthentication();
app.MapHub<Hubtel>("/hubtel");
app.UseAuthorization();
// app.UseHangfireDashboard();
//app.UseRouting();
//app.UseEndpoints(endpoints =>
//{
//    endpoints.MapControllers();
//    endpoints.MapHangfireDashboard();
//});

app.MapControllers();

app.Run();
